{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 随机抽样函数",
   "id": "7955d244cc2716b8"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 1.torch.seed()\n",
    "用于生成不确定的随机数,返回一个64位的数值\n",
    "\n",
    "例如,生成一个64位的随机数,代码如下："
   ],
   "id": "7e675545b7315612"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T06:21:02.492713Z",
     "start_time": "2025-03-04T06:21:02.481742Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "torch.seed()"
   ],
   "id": "226d25eb433a6e03",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21528536216000"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 2. torch.manual_seed()\n",
    "设定生成随机数的种子,并返回一个torch.Generator对象。\n",
    "例如,为了确保生成的随机数都是固定的,可以使用torch.manual_seed()函数,代码如下："
   ],
   "id": "914fc5c9e05c2ea7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T06:21:09.520914Z",
     "start_time": "2025-03-04T06:21:09.510936Z"
    }
   },
   "cell_type": "code",
   "source": "torch.manual_seed(12)",
   "id": "27e9ad46f04cc17d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<torch._C.Generator at 0x24a25d37dd0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 3. torch.initial_seed()\n",
    "返回生成随机数的原始种子值。\n",
    "例如,生成一个原始种子,代码如下：\n"
   ],
   "id": "7833929a046cd6a8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T05:16:09.038036Z",
     "start_time": "2025-03-04T05:16:09.031053Z"
    }
   },
   "cell_type": "code",
   "source": "torch.initial_seed()",
   "id": "252dd88ff0dbe8b2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 4. torch.get_rng_state()\n",
    "返回随机生成器状态（Byte Tensor）。\n",
    "例如，生成一个随机生成器状态，代码如下："
   ],
   "id": "5d9d4ac7e124b250"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T05:16:09.084907Z",
     "start_time": "2025-03-04T05:16:09.076928Z"
    }
   },
   "cell_type": "code",
   "source": "torch.get_rng_state()",
   "id": "3a8ab3613a0865f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([12,  0,  0,  ...,  0,  0,  0], dtype=torch.uint8)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### 5.torch.set_rng_state(new_state)\n",
    "设定随机生成器状态。\n",
    "例如，设定一个随机生成器状态，代码如下："
   ],
   "id": "6b3bbd97e698aa7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-04T05:16:09.127793Z",
     "start_time": "2025-03-04T05:16:09.111834Z"
    }
   },
   "cell_type": "code",
   "source": [
    "rng_state1 = torch.get_rng_state()\n",
    "print(rng_state1)\n",
    "\n",
    "torch.set_rng_state(rng_state1*2)\n",
    "rng_state2 = torch.get_rng_state()\n",
    "print(rng_state2)"
   ],
   "id": "bf6960160cc31d37",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([12,  0,  0,  ...,  0,  0,  0], dtype=torch.uint8)\n",
      "tensor([24,  0,  0,  ...,  0,  0,  0], dtype=torch.uint8)\n"
     ]
    }
   ],
   "execution_count": 12
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
